wx.ready(function () {
    var imgSrc = []; //图片路径
    var imgDeal= [];//处理点
    var imgFile = []; //文件流
    var imgFile1 = []; //文件流
    var imgName = []; //图片名字
    var imgBox = '#imgBox';
    var imgBox1 = '#imgBox1';
    // 5 图片接口
    // 5.1 拍照、本地选图
    var images = {
        localId: [],
        serverId: []
    };

    //检查表图片上传
    // document.querySelector('#chooseImage').onclick = function () {
    //     var localId = "";
    //     wx.chooseImage({
    //         count: 1, // 默认9
    //         sizeType:'compressed',
    //         success: function (res) {
    //             images.localId = res.localIds;
    //             localId = images.localId[0];
    //             $('#table').attr("src",images.localId[0]);
    //             //    $('#imgs').attr("style","display:none")
    //             //$('#pic_url').val(images.localId[0]);
    //
    //         }
    //     });
    // };


    //隐患点图片上传
    document.querySelector('#chooseImage1').onclick = function () {
        var localIds = "";
        wx.chooseImage({
            //count: 9,  默认9
            sizeType:'compressed',
            success: function (res) {
                // localId = res.localIds;
                // //localId = images.localId;
                // imgSrc = localId.toString().split(",");
                imgSrc = res.localIds.toString().split(",");
                addNewContent1(imgBox);
                syncUpload(imgSrc);
            }
        });
    };


    ////隐患点 2图片上传
    document.querySelector('#chooseImage2').onclick = function () {
        var localIds = "";
        wx.chooseImage({
            //count: 9,  默认9
            sizeType:'compressed',
            success: function (res) {
                images.localId = res.localIds;
                //localId = images.localId;
                imgSrc = images.localId;
                addNewContent1(imgBox1);
                syncUpload1(imgSrc); //设置到dangerUrl1 里面
            }
        });
    };

    //隐患点上传图片到服务器
    function syncUpload(localIds){
        var localId = localIds.pop();
        wx.uploadImage({
            localId: localId,
            isShowProgressTips: 1,
            sizeType:'compressed',
            success: function (res) {
                var serverId = res.serverId; // 返回图片的服务器端ID
                imgFile.push(serverId);
                //其他对serverId做处理的代码
                if(localIds.length == 0) {
                    $("#danUrls").val(imgFile);

                }
                if (localIds.length > 0) {
                    syncUpload(localIds);
                }
            }
        });
    }

    //处理点上传图片到服务器
    function syncUpload1(localIds){
        var localId = localIds.pop();
        wx.uploadImage({
            localId: localId,
            isShowProgressTips: 1,
            sizeType:'compressed',
            success: function (res) {
                var serverId = res.serverId; // 返回图片的服务器端ID
                imgFile1.push(serverId);
                //其他对serverId做处理的代码
                if(localIds.length == 0) {
                    $("#dealUrls").val(imgFile1);
                }
                if (localIds.length > 0) {
                    syncUpload1(localIds);
                }
            }
        });
    }

    //隐患点图片展示
    function addNewContent1(obj) {
        $(obj).html("");
        var ua = navigator.userAgent.toLowerCase();
        var ios;
        console.log(ua);
        if (/iphone|ipad|ipod/.test(ua)) {
            ios = true;
        } else {
            ios = false;
        }
        //ios
        if (ios || window.__wxjs_is_wkwebview) {
            for(var a = 0; a < imgSrc.length; a++) {
                wx.getLocalImgData({
                    localId: imgSrc[a],
                    success: function (res) {
                        var localData = res.localData; // localData是图片的base64数据，可以用img标签显示var str = '<div class="shangchuan_img_xx">'+
                        var oldBox = $(obj).html();
                        $(obj).html(oldBox + '<div class="imgContainer"><img src=' + localData + ' onclick="imgDisplay(this)"></div>');
                    },
                    fail: function (res) {
                        alert('该图片暂时无法查看')
                    }
                });
            }
        }else{
            for(var a = 0; a < imgSrc.length; a++) {
                var oldBox = $(obj).html();
                $(obj).html(oldBox + '<div class="imgContainer"><img  src=' + imgSrc[a] + ' onclick="imgDisplay(this)"></div>');
            }
        }
    }

    //处理点图片展示
    function addNewContent2(obj) {
        $(obj).html("");
        for(var a = 0; a < imgSrc.length; a++) {
            var oldBox = $(obj).html();
            $(obj).html(oldBox + '<div class="imgContainer"><img  src=' + imgSrc[a] + ' onclick="imgDisplay(this)"></div>');
        }
    }


    function uploadImg(localId){
        wx.uploadImage({
            localId:localId,
            success: function (res) {
                $('#media_id').val(res.serverId);
                //  document.forms[0].submit();
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
    }

    //隐患点图片上传
    function uploadMoreImg(localId){
        var i = 0, length = localId.length;
        images.serverId = [];
        function upload() {
            wx.uploadImage({
                localId: localId[i],
                success: function (res) {
                    i++;
                    //alert('已上传：' + i + '/' + length);
                    images.serverId.push(res.serverId);
                    if (i < length) {
                        upload();
                    }
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }

            });
        }
        upload();
    }

    // 5.2 图片预览
    document.querySelector('#previewImage').onclick = function () {
        wx.previewImage({
            current:  $('#table').attr("src"),
            urls: [
                'http://img3.douban.com/view/photo/photo/public/p2152117150.jpg',
                'http://img5.douban.com/view/photo/photo/public/p1353993776.jpg',
                'http://img3.douban.com/view/photo/photo/public/p2152134700.jpg'
            ]
        });
    };

    // 5.3 上传图片
    document.querySelector('#uploadImage').onclick = function () {
        if (images.localId.length == 0) {
            alert('请先使用 chooseImage 接口选择图片');
            return;
        }
        var i = 0, length = images.localId.length;
        images.serverId = [];
        function upload() {
            wx.uploadImage({
                localId: images.localId[i],
                success: function (res) {
                    i++;
                    //alert('已上传：' + i + '/' + length);
                    images.serverId.push(res.serverId);
                    if (i < length) {
                        upload();
                    }
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });
        }
        upload();
    };

    // 5.4 下载图片
    document.querySelector('#downloadImage').onclick = function () {
        if (images.serverId.length === 0) {
            alert('请先使用 uploadImage 上传图片');
            return;
        }
        var i = 0, length = images.serverId.length;
        images.localId = [];
        function download() {
            wx.downloadImage({
                serverId: images.serverId[i],
                success: function (res) {
                    i++;
                    alert('已下载：' + i + '/' + length);
                    images.localId.push(res.localId);
                    if (i < length) {
                        download();
                    }
                }
            });
        }
        download();
    };
});
